package seedpod.kb2db.protegeplugin;
/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
import java.io.File;
import java.net.URI;

import javax.swing.*;

import seedpod.kb2db.ProjectTransformValidator;
import seedpod.kb2db.protegeplugin.action.*;
import edu.stanford.smi.protege.*;
import edu.stanford.smi.protege.model.*;
import edu.stanford.smi.protege.plugin.*;
import edu.stanford.smi.protege.ui.*;
import edu.stanford.smi.protege.util.ComponentFactory;
import edu.stanford.smi.protege.util.PropertyList;
import edu.stanford.smi.protege.util.URIUtilities;
import edu.stanford.smi.protege.storage.clips.ClipsKnowledgeBaseFactory;

public class SeedpodProjectPlugin extends AbstractProjectPlugin
{
	public static final String ACTION_NEW = "New Seedpod Project...";
	public static final String ACTION_CONVERT = "Convert Current Project";
	public static final String ACTION_VALIDATE = "Validate Project";
	public static final String ACTION_EXPORT = "Export To SQL Schema";

	private static URI _TMP_PROJECT_URI;

	private JMenu spMenu;

	public void afterShow( ProjectView view, ProjectToolBar toolBar, ProjectMenuBar menuBar ) {
		if ( ProjectTransformValidator.isClipsKB( view.getProject() ) )
			createSeedpodMenu( menuBar );
	}

	private void createSeedpodMenu( ProjectMenuBar menuBar ) {
		spMenu = ComponentFactory.createMenu( "Seedpod" );
		createMenuItem( spMenu, new CreateSeedpodProject() );
		createMenuItem( spMenu, new ConvertToSeedpodProject() );
		createMenuItem( spMenu, new ValidateSeedpodProject() );
		spMenu.addSeparator();
		createMenuItem( spMenu, new ExportToSQL() );
		menuBar.add( spMenu );
	}

	private static void createMenuItem( JMenu menu, Action action ) {
		ComponentFactory.addMenuItem( menu, action );
	}

	public void afterCreate( Project p ) {
	// do nothing
	}

	public void afterSave( Project p ) {}

	public void afterLoad( Project p ) {}

	public void beforeSave( Project p ) {}

	public void beforeHide( ProjectView view, ProjectToolBar toolBar, ProjectMenuBar menuBar ) {}

	public void beforeClose( Project p ) {
		cleanupTmpProjectFiles( p );
	}

	private void cleanupTmpProjectFiles( Project p ) {
		if ( _TMP_PROJECT_URI == null )
			return;

		System.out.println( "cleaning temp project files: " + p.getProjectURI() );
		PropertyList sources = p.getSources();
		File pFile = new File( _TMP_PROJECT_URI.getPath() );
		File clsFile = new File( URIUtilities.getParentURI( _TMP_PROJECT_URI ).getPath() + "/"
				+ ClipsKnowledgeBaseFactory.getClsesSourceFile( sources ) );
		File insFile = new File( URIUtilities.getParentURI( _TMP_PROJECT_URI ).getPath() + "/"
				+ ClipsKnowledgeBaseFactory.getInstancesSourceFile( sources ) );
		
		if ( pFile.delete() ) {
			if ( clsFile != null )
				clsFile.delete();
			if ( insFile != null )
				insFile.delete();
		}
	}

	public static void registerTmpProject( URI tmpUri ) {
		_TMP_PROJECT_URI = tmpUri;
	}

	public String getName() {
		return "Seedpod Project Plugin";
	}

	/**
	 * This is a CLIPS project.
	 * 
	 * @param p
	 * @param errors
	 * @return
	 */
	/*
	 * public boolean isSuitable( Project p, Collection errors ) { boolean
	 * isSuitable = true; isSuitable = ProjectTransformValidator.isClipsKB( p );
	 * return isSuitable; }
	 */

	public void dispose() {
	// do nothing
	}

	public static void main( String[] args ) {
		Application.main( args );
	}

}
